<?php
	///////////////////////////////////////////////////////////////////////////////////////////////////
	//	PREVIOUS TIME
	//	WRITTEN BY: TRAVIS THORNTON
	//	12-07-2009
	//
	//	What this page does:
	//	1) Shows all timesheets for a emplyee
	//	2) Manager can also view their employee's timesheets from this page
	//
	////////////////////////////////////////////////////////////////////////////////////////////////////
	
	session_start();
	require_once 'php_includes.php';
	require_once 'time_functions.php';

	//Make sure logged in
	checkLogin();

	//This is the user we are looking up (defaults to logged in user)
	if(!$_POST['usertime'])
	{
		$user = $_SESSION['tmsUserID'];
	}
	else//otherwise, it has to be a manager
	{
		$user = $_POST['usertime'];
	}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
  <head>
    <title>Payroll and Timesheet Management Website</title>
    <link rel="stylesheet" type="text/css" href="styles.css"/>
    <script type="text/javascript" src = "jsfunctions.js">
    </script>
  </head>
  <body>
  
	<?php writeHeader(); ?>
	
	<?php
	
	sqlConnect();
	//if we are not looking up the logged in user, assume that it is a manager looking at this page
	if($user != $_SESSION['tmsUserID'])
	{
		$sql = "SELECT ManagerID, EmployeeFName, EmployeeLName FROM Employee WHERE EmployeeID = '" . $user . "'";
		$results = sqlQuery($sql);
		
		if(mysql_result($results, 0, 0) != $_SESSION['tmsUserID'])
		{
			die("You don't have permission to view this page.");
		}
	}
	
	echo "<p><span class=\"pageheader\">View ";
	
	//display user if manager logged in 
	if($user != $_SESSION['tmsUserID'])
	{
		echo mysql_result($results, 0, 1) . " " . mysql_result($results, 0, 2) . " (" . $user . ")'s ";
	}
	else
	{
		echo "my ";
	}
		
	
	echo " timesheets</span><br /></p><br />";
	echo "<table class=\"justified\" width=\"50%\">";
	echo "<tr>";
	echo "<th></th>";
	echo "<th>Pay Period</th>";
	echo "<th>Total Hours</th>";
	echo "<th>Status</th>";
	echo "</tr>";
	
	//get all period info
	$sql = "SELECT PeriodStart, PeriodEnd, PeriodStatus FROM PayPeriod WHERE EmployeeID = '" . $user . "' ORDER BY PeriodStart DESC";
	$results = sqlQuery($sql);
	
	$num_rows = mysql_num_rows($results);
	
	sqlExit();
	
	if($num_rows == 0 )//no information
	{
		echo "<tr>\n";
		echo "<td class=\"record\" colspan=\"4\">No Records Available</td>";
		echo "</tr>";
	}
	else//fill out table
	{
		for($i = 0; $i < $num_rows; ++$i)
		{
			$data = mysql_fetch_row($results);
			
			echo "<tr>\n";
			echo "<td class=\"view\"><form action=\"timesheet.php\" method=\"post\"><div>";
			
			//form data for the timesheet
			echo "<input type=\"hidden\" name=\"usertime\"value=\"" . $user . "\" />";
			echo "<input type=\"hidden\" name=\"period\"value=\"" . $data[0] . "\" />";
			
			echo "<input type=\"submit\" value=\"View\" class=\"button\" /></div></form></td>\n";
			echo "<td class=\"time_record\">";
			
			//Format the mysql date correctly
			//10/18/2009 - 10/31/2009
			$date1 = explode("-", $data[0]);
			$date2 = explode("-", $data[1]);
			echo $date1[1] . "/" . $date1[2] . "/" . $date1[0] . " - " . $date2[1] . "/" . $date2[2] . "/" . $date2[0];
			
			echo "</td>\n";
			echo "<td class=\"time_record\">";
			
			//Get total time for this period
			generate2WeekTotals($user, $data[0]);//generate the total time by passing in information to extract the row
			echo getBasic2WeekTotal();//report the total hours
			resetGlobalTime();//reset the globals for the next row
			
			echo "</td>\n";
			echo "<td class=\"time_record\">";
			
			//parse status info
			switch($data[2])
			{
				case 0:
					echo "Current";
				break;
				case 1:
					echo "Pending Approval";
				break;
				case 2:
					echo "Approved";
				break;
				default:
					echo "Error";
			}
			
			echo "</td>\n";
			echo "</tr>\n";
		}
	}
	?>
	
	</table>
  </body>
</html>